package com.yanqu.road.server.cmd.marriage;

import com.yanqu.road.entity.player.UserMarriageData;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.MarriagePb;
import com.yanqu.road.entity.marriage.MarriageRecord;
import com.yanqu.road.entity.marriage.MarriageResult;
import com.yanqu.road.pb.marriage.MarriageProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.MarriageModule;
import com.yanqu.road.server.manger.CrossUnionServerGroupMgr;
import com.yanqu.road.server.manger.marriage.MarriageMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = Protocol.S_MARRIAGE_APPLY_DEAL, desc = "处理联姻请求")
public class MarriageApplyDealCmd extends AbstractCommand {

    private String myChildUid;
    private String partnerChildUid;
    private int type;      //类型 1：全服； 2：指定；  3：一键拒绝； 4：商会
    private boolean agree;
    private MarriageResult result;
    private long partnerServerId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        MarriageProto.MarriageApplyDealReqMsg reqMsg = MarriageProto.MarriageApplyDealReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        if(reqMsg.hasPartnerServerId()){
            partnerServerId = reqMsg.getPartnerServerId();
        }
        if(1 == type || 2 == type || 4 == type) {
            myChildUid = reqMsg.getMyChildUid();
            partnerChildUid = reqMsg.getPartnerChildUid();
            agree = reqMsg.getAgree();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (1 == type) {

            //前n次联姻，强制npc,即本服联姻
            UserMarriageData userMarriageData = player.getModule(MarriageModule.class).getUserMarriageData();
            long marriageTimes = 0;
            if(null != userMarriageData) {
                marriageTimes = userMarriageData.getTotalMarriageTimes();
            }

            long serverId = ConfigHelper.getLong("serverId");
            if(CrossUnionServerGroupMgr.getGroupServerCountByServerId(serverId) > 1 && marriageTimes >= GameConfig.MARRIAGE_CAN_NPC_TIMES){
                //跨商会分组
                result = MarriageMgr.dealCrossWorldMarriage(player, myChildUid, partnerChildUid, partnerServerId);
            }else {
                result = MarriageMgr.dealServerMarriage(player, myChildUid, partnerChildUid);
            }
        } else if (2 == type) {
            result = MarriageMgr.dealAppointMarriage(player, myChildUid, partnerChildUid, agree);
        } else if (3 == type) {
            result = MarriageMgr.refuseAllAppointMarriage(player);
        } else {
            result = new MarriageResult();
            result.setRet(GameErrorCode.E_MARRIAGE_WRONG_TYPE);
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        //特定条件下，不返回给客户端
        if(result.getRet() == -1){
            return;
        }
        MarriageProto.MarriageApplyDealRespMsg.Builder respMsg = MarriageProto.MarriageApplyDealRespMsg.newBuilder();
        if (null == result) {
            respMsg.setRet(GameErrorCode.E_MARRIAGE_WRONG_TYPE);
        }else {
            respMsg.setRet(result.getRet());
            if(1 == type || (2 == type && agree) || (4 == type && agree)) {
                MarriageRecord marriageRecord = result.getMarriageRecord();
                if (null != marriageRecord) {
                    String reward = PropertyHelper.parsePropertyToString(marriageRecord.getPartnerReward());
                    if(null == reward){
                        reward = "";
                    }
                    respMsg.setReward(reward);
                    MarriageProto.MarriageRecordTempMsg.Builder msg = MarriagePb.parseMarriageRecordTempMsg(marriageRecord);
                    if(null != msg) {
                        respMsg.setMarriageData(msg);
                    }
                }
            }
        }
        player.sendPacket(Protocol.U_MARRIAGE_APPLY_DEAL, respMsg);
    }
}
